package com.tanxi521.afkreward.util;

import com.tanxi521.afkreward.AfkReward;
import com.tanxi521.afkreward.placeholder.AfkPlaceHolder;
import com.tanxi521.afkreward.pojo.Area;
import com.tanxi521.afkreward.pojo.PlayerAfk;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class ConfigUtil {

    public static FileConfiguration langYml;

    public static LinkedList<Area> areas = new LinkedList<>();

    public static boolean isMysql() {
        return AfkReward.getInstance().getConfig().getBoolean("mysql.enable");
    }

    public static int getPoolSize() {
        return AfkReward.getInstance().getConfig().getInt("mysql.pool");
    }

    public static String getMysqlUrl() {
        return AfkReward.getInstance().getConfig().getString("mysql.url");
    }

    public static String getMysqlPort() {
        return AfkReward.getInstance().getConfig().getString("mysql.port");
    }

    public static String getMysqlUser() {
        return AfkReward.getInstance().getConfig().getString("mysql.user");
    }

    public static String getMysqlPassword() {
        return AfkReward.getInstance().getConfig().getString("mysql.password");
    }

    public static String getMysqlDatabase() {
        return AfkReward.getInstance().getConfig().getString("mysql.database");
    }

    public static String getLanguage() {
        return AfkReward.getInstance().getConfig().getString("language");
    }

    public static int getRound() {
        return AfkReward.getInstance().getConfig().getInt("reward.round");
    }

    public static void initMsg() {
        File lang = new File(AfkReward.getInstance().getDataFolder(), "language" + File.separator + getLanguage());
        try {
            if (!lang.exists()) {
                AfkReward.getInstance().saveResource("language" + File.separator + lang.getName(), true);
                lang = new File(AfkReward.getInstance().getDataFolder(), "language" + File.separator + getLanguage());
            }
            langYml = YamlConfiguration.loadConfiguration(lang);
            AfkReward.getInstance().getLogger().info("§e导入语言文件成功");
        } catch (Exception e) {
            AfkReward.getInstance().getLogger().warning("§e导入语言文件失败，请检查配置文件");
            AfkReward.getInstance().getPluginLoader().disablePlugin(AfkReward.getInstance());
        }
    }

    public static List<String> getPluginHelp() {
        return langYml.getStringList("help");
    }

    public static int getMaxNum() {
        return AfkReward.getInstance().getConfig().getInt("reward.player.limit");
    }

    public static int getMaxRest() {
        return AfkReward.getInstance().getConfig().getInt("reward.max.limit");
    }

    public static boolean isMaxLimit() {
        return AfkReward.getInstance().getConfig().getBoolean("reward.max.enable");
    }

    public static boolean isPlayerLimit() {
        return AfkReward.getInstance().getConfig().getBoolean("reward.player.enable");
    }

    public static String getTypeDes(String typeName) {
        return AfkReward.getInstance().getConfig().getString("type." + typeName + ".describe");
    }

    public static List<String> getPermissionCommand(CommandSender sender, String typeName) {
        ConfigurationSection configurationSection =
                AfkReward.getInstance().getConfig().getConfigurationSection("type." + typeName);
        List<String> permission = configurationSection.getKeys(false)
                .stream().filter(a -> !a.equals("describe")).collect(Collectors.toList());
        for (String typeCommand : permission) {
            if (sender.hasPermission("AfkRewards." + typeCommand)) {
                return AfkReward.getInstance().getConfig().getStringList("type." + typeName + "." + typeCommand);
            }
        }
        AfkReward.getInstance().getLogger().warning("未发现玩家符合任何一条奖励权限");
        return new ArrayList<String>();
    }

    public static List<String> getVIPCommands(String typeName) {
        return AfkReward.getInstance().getConfig().getStringList("type." + typeName + ".vip");
    }

    public static List<String> getAllType() {
        return new LinkedList<>(AfkReward.getInstance().getConfig().getConfigurationSection("type").getKeys(false));
    }

    public static boolean getEnableFree() {
        return AfkReward.getInstance().getConfig().getBoolean("reward.enableAfk");
    }

    public static boolean getEnableArea() {
        return AfkReward.getInstance().getConfig().getBoolean("reward.enableArea");
    }

    public static List<String> getCommand(CommandSender sender, PlayerAfk playerAfk) {
        String type = playerAfk.getType();
        if (type.equals("自由")) {
            return getPermissionCommand(sender, playerAfk.getReward());
        } else if (type.equals("区域")) {
            String typeName = AfkReward.getInstance().getConfig().getString("area." + playerAfk.getReward() + ".type");
            return getPermissionCommand(sender, typeName);
        }
        return null;
    }

    public static void initArea() {
        Set<String> areaNames = AfkReward.getInstance().getConfig().getConfigurationSection("area").getKeys(false);
        for (String areaName : areaNames) {
            Area area = new Area();
            area.setName(areaName);
            area.setWorld(AfkReward.getInstance().getConfig().getString("area." + areaName + ".pos.world"));
            List<String> posList = AfkReward.getInstance().getConfig().getStringList("area." + areaName + ".pos.posList");
            MapPoint[] mapPoints = new MapPoint[posList.size()];
            try {
                for (int i = 0; i < posList.size(); i++) {
                    String pos = posList.get(i);
                    String[] split = pos.split(",");
                    MapPoint mapPoint = new MapPoint();
                    mapPoint.setLng(Double.parseDouble(split[0]));
                    mapPoint.setLat(Double.parseDouble(split[1]));
                    mapPoints[i] = mapPoint;
                }
            } catch (Exception e) {
                e.printStackTrace();
                AfkReward.getInstance().getLogger().warning("§c§l初始化挂机区域失败，请检查配置文件");
            }
            area.setMapPoints(mapPoints);
            area.setEnable(AfkReward.getInstance().getConfig().getBoolean("area." + areaName + ".enable"));
            areas.add(area);
        }
    }

    public static void initPlaceHolder() {
        AfkReward.getInstance().getLogger().info(new AfkPlaceHolder().register() ? "§e初始化PlaceHolderAPI变量成功" : "§c§l初始化PlaceHolderAPI变量失败");
    }

    public static void checkVersion() {
        String version = AfkReward.getInstance().getConfig().getString("version");
        if (!version.equals(AfkReward.getInstance().getDescription().getVersion())) {
            AfkReward.getInstance().getLogger().warning("§c§l配置文件版本错误，请备份后重启插件生成新的配置文件");
            AfkReward.getInstance().getPluginLoader().disablePlugin(AfkReward.getInstance());
            throw new RuntimeException("配置文件版本错误，备份后删除当前config.yml，并重新填写");
        }
    }

}
